package com.ruoyi.web.controller.mall;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ShoppingCart;
import com.ruoyi.system.service.IShoppingCartService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品购物车Controller
 * 
 * @author ruoyi
 * @date 2024-03-22
 */
@Controller
@RequestMapping("/mall/shoppingCart")
public class ShoppingCartController extends BaseController
{
    private String prefix = "mall/shoppingCart";

    @Autowired
    private IShoppingCartService shoppingCartService;

    @RequiresPermissions("mall:shoppingCart:view")
    @GetMapping()
    public String shoppingCart()
    {
        return prefix + "/shoppingCart";
    }

    /**
     * 查询商品购物车列表
     */
    @RequiresPermissions("mall:shoppingCart:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ShoppingCart shoppingCart)
    {
        startPage();
        List<ShoppingCart> list = shoppingCartService.selectShoppingCartList(shoppingCart);
        return getDataTable(list);
    }

    /**
     * 导出商品购物车列表
     */
    @RequiresPermissions("mall:shoppingCart:export")
    @Log(title = "商品购物车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ShoppingCart shoppingCart)
    {
        List<ShoppingCart> list = shoppingCartService.selectShoppingCartList(shoppingCart);
        ExcelUtil<ShoppingCart> util = new ExcelUtil<ShoppingCart>(ShoppingCart.class);
        return util.exportExcel(list, "商品购物车数据");
    }

    /**
     * 新增商品购物车
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存商品购物车
     */
    @RequiresPermissions("mall:shoppingCart:add")
    @Log(title = "商品购物车", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ShoppingCart shoppingCart)
    {
        return toAjax(shoppingCartService.insertShoppingCart(shoppingCart));
    }

    /**
     * 修改商品购物车
     */
    @RequiresPermissions("mall:shoppingCart:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ShoppingCart shoppingCart = shoppingCartService.selectShoppingCartById(id);
        mmap.put("shoppingCart", shoppingCart);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品购物车
     */
    @RequiresPermissions("mall:shoppingCart:edit")
    @Log(title = "商品购物车", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ShoppingCart shoppingCart)
    {
        return toAjax(shoppingCartService.updateShoppingCart(shoppingCart));
    }

    /**
     * 删除商品购物车
     */
    @RequiresPermissions("mall:shoppingCart:remove")
    @Log(title = "商品购物车", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(shoppingCartService.deleteShoppingCartByIds(ids));
    }
}
